package com.raysun.androidbase.interaction

import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.CalendarContract
import android.provider.CalendarContract.Events
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.raysun.androidbase.databinding.ActivityImplicitlyIntentBinding
import java.util.Calendar

class ImplicitlyIntentActivity : AppCompatActivity() {

    private lateinit var binding: ActivityImplicitlyIntentBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityImplicitlyIntentBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // 构建隐式Intent
        implicitlyBuildIntent()

        // 向Intent添加额外的数据
        addExtraDataToIntent()
    }

    private fun addExtraDataToIntent() {
        binding.makeEmail.setOnClickListener {
            Intent(Intent.ACTION_SEND).apply {
                type = "text/plain"
                putExtra(Intent.EXTRA_EMAIL, arrayOf("ray@example.com"))
                putExtra(Intent.EXTRA_SUBJECT, "Email Subject")
                putExtra(Intent.EXTRA_TEXT, "Email Message Text")
            }.let {
                startActivity(it)
                // 应用选择器
                // 这会显示一个对话框，其中包含可响应向 createChooser() 方法传递的 intent 的应用列表。如果操作不是 ACTION_SEND 或 ACTION_SEND_MULTIPLE，则可以提供 title 参数
                // startActivity(Intent.createChooser(it, null))
            }
        }

        binding.makeCalendar.setOnClickListener {
            Intent(Intent.ACTION_INSERT, Events.CONTENT_URI).apply {
                val beginTime: Calendar = Calendar.getInstance().apply {
                    set(2023, Calendar.SEPTEMBER, 23, 0, 0)
                }
                val endTime = Calendar.getInstance().apply {
                    set(2023, Calendar.SEPTEMBER, 23, 0, 0)
                }
                putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.timeInMillis)
                putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.timeInMillis)
                putExtra(Events.TITLE, "生日")
                putExtra(Events.EVENT_LOCATION, "Su Zhou")
            }.let {
                startActivity(it)
            }
        }
    }

    private fun implicitlyBuildIntent() {
        binding.makeCall.setOnClickListener {
            val callIntent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:13965309426"))
            startActivity(callIntent)
        }

        binding.makeMap.setOnClickListener {
            val mapIntent = Intent(
                Intent.ACTION_VIEW,
                Uri.parse("geo:37.422219,-122.08364?z=14")
            )
            try {
                startActivity(mapIntent)
            } catch (e: ActivityNotFoundException) {
                Log.d("Raysunshine", "implicitlyBuildIntent: ${e.message}")
            }
        }

        binding.makeInternet.setOnClickListener {
            val internetIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"))
            startActivity(internetIntent)
        }


    }
}